package com.orangelife.webservice.service.propFeeCommInfo.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeCommInfo.IPropFeeCommInfoService;
import com.orangelife.service.propFeeCommInfo.model.PropFeeCommInfoDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.base.BaseService;
import com.orangelife.webservice.base.PageUtils;
import com.orangelife.webservice.service.propFeeCommInfo.IPropFeeCommInfoWebService;
import com.orangelife.webservice.service.propFeeCommInfo.entity.SearchCommInfo;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation 2015. All Rights Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * propFeeCommInfo类 的WebService基本作业
 * 
 * @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-06 17:13:39
 * ==============================================================
 */
@Service("webservice_propFeeCommInfo")
public class PropFeeCommInfoWebServiceImpl extends BaseService implements
		IPropFeeCommInfoWebService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "propFeeCommInfoService")
	private IPropFeeCommInfoService propFeeCommInfoService;

	@Override
	public Response getAllPropFeeCommInfo(HttpServletRequest servletRequest,
			SearchCommInfo searchCommInfo) throws Exception {

		String commID = searchCommInfo.getCommID();
		String code = searchCommInfo.getCode();
		List<String> codes = searchCommInfo.getCodes();
		Map<String, Object> result = new HashMap<String, Object>();

		/************************************************
		 * 查询列表
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("code", code);
		if (codes != null && codes.size() > 0) {
			map.put("codes", codes);
		}
		List<Map<String, Object>> list = propFeeCommInfoService
				.findPropFeeCommInfo(map);

		list = PropertyUtils.turnToEntity(list);

		for (Map<String, Object> map2 : list) {
			result.put(map2.get("code") + "", map2);
		}

		/************************************************
		 * 返回结果设定
		 ************************************************/

		return this.normalResp(result);
	}

	@Override
	public Response addPropFeeCommInfo(HttpServletRequest servletRequest,
			PropFeeCommInfoDto propFeeCommInfo) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		int result = propFeeCommInfoService.addPropFeeCommInfo(propFeeCommInfo);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "添加实体成功");

		return this.normalResp();
	}

	@Override
	public Response editPropFeeCommInfo(HttpServletRequest servletRequest,
			PropFeeCommInfoDto propFeeCommInfo) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(propFeeCommInfo.getInfoID(),
				"infoID");
		if (error != null) {
			return error;
		}

		/************************************************
		 * 输入参数检查
		 ************************************************/
		// TODO;

		int result = propFeeCommInfoService
				.editPropFeeCommInfo(propFeeCommInfo);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response deletePropFeeCommInfo(HttpServletRequest servletRequest,
			String infoID) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(infoID, "infoID");
		if (error != null) {
			return error;
		}
		int result = propFeeCommInfoService
				.deletePropFeeCommInfoByLogic(infoID);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "删除实体成功");

		return this.normalResp();
	}

	@Override
	public Response findPropFeeCommInfoByID(HttpServletRequest servletRequest,
			String infoID) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(infoID, "infoID");
		if (error != null) {
			return error;
		}
		PropFeeCommInfoDto propFeeCommInfo = propFeeCommInfoService
				.findPropFeeCommInfoByID(infoID);

		Map<String, Object> maps = new HashMap<String, Object>();

		if (propFeeCommInfo != null) {

		}

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "查询实体成功");
		return this.normalResp(propFeeCommInfo);
	}

	@Override
	public Response getAllPropFeeCommInfo(HttpServletRequest servletRequest,
			String type, String commID, String search, String tsCreate,
			String pageIndex, String pageSize) throws Exception {

		PageUtils pageUtils = this.validatePageAndSetPage(servletRequest,
				pageIndex, pageSize);
		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (!StringUtils.isBlank(tsCreate)) {
			StringUtils.isTime(tsCreate);
			tsCreate = String.valueOf(Long.parseLong(tsCreate) / 1000);
		}
		/************************************************
		 * 查询列表
		 ************************************************/
		List<Map<String, Object>> list = propFeeCommInfoService
				.findPropFeeCommInfoByPage(type, commID,
						tsCreate == null ? null : tsCreate,
						search == null ? null : search, pageUtils
								.getiPageIndex(), pageUtils.getiPageSize());

		/************************************************
		 * 返回结果设定
		 ************************************************/
		Long total = ((Page<Map<String, Object>>) list).getTotal();

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginName() + new Date()
				+ "查询实体成功");

		return this.normalResp(pageUtils.getiPageIndex(),
				pageUtils.getiPageSize(), total,
				PropertyUtils.turnToEntity(list));
	}

	@Override
	public Response bauthFindPropFeeCommInfo(HttpServletRequest servletRequest,
			List<String> propFeeCommInfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeCommInfo == null || propFeeCommInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", propFeeCommInfoService
					.bauthfindPropFeeCommInfo(propFeeCommInfo));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthFindPropFeeCommInfos(
			HttpServletRequest servletRequest,
			Map<String, Object> propFeeCommInfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeCommInfo == null || propFeeCommInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", PropertyUtils
					.turnToEntity(propFeeCommInfoService
							.bauthfindPropFeeCommInfoByMap(propFeeCommInfo)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response clearCache(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		propFeeCommInfoService.clearCache();
		return this.normalResp();
	}

	@Override
	public Response getFeePayWay(HttpServletRequest servletRequest,
			String commID) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 获取小区缴费的缴费的付款方式
		 ************************************************/
		try {
			if (StringUtils.isBlank(commID)) {
				throw new AppBizException(2, "亲，小区信息");
			}
			map.put("pageEntity", propFeeCommInfoService
					.findPropFeeCommInfoByRelevantID(null, commID, null, null,
							null, "1"));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthPropFeeCommInfo(HttpServletRequest servletRequest,
			List<PropFeeCommInfoDto> propFeeCommInfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeCommInfo == null || propFeeCommInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}
			propFeeCommInfoService.addPropFeeCommInfo(propFeeCommInfo);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public synchronized Response initCommInfo(
			HttpServletRequest servletRequest,
			List<PropFeeCommInfoDto> propFeeCommInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		/**
		 * 
		 * */

		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeCommInfo == null || propFeeCommInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			Map<String, Object> param = new HashMap<String, Object>();
			param.put("commID", propFeeCommInfo.get(0).getCommID());
			param.put("code", "curDate");
			Map<String, Map<String, Object>> p = propFeeCommInfoService
					.findPropFeeCommInfo(propFeeCommInfo.get(0).getCommID());

			boolean flag = false;
			if (p.get("curDate") != null && p.get("initDate") != null) {

				if (!p.get("curDate").get("val")
						.equals(p.get("initDate").get("val"))) {

					flag = true;
				}

			}

			if (p != null) {
				for (PropFeeCommInfoDto pp : propFeeCommInfo) {

					if (flag) {

						if (pp.getCode().equals("initDate")
								|| pp.getCode().equals("curDate")) {

							continue;
						}

					}

					if (p.get(pp.getCode()) != null) {

						pp.setInfoID(p.get(pp.getCode()).get("info_id")
								.toString());

						propFeeCommInfoService.editPropFeeCommInfo(pp);

					} else {

						propFeeCommInfoService.addPropFeeCommInfo(pp);

					}

				}
			}

			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();
	}

	@Override
	public Response getCurTime(HttpServletRequest servletRequest, String commID)
			throws Exception {
		Response error = this.validateUUId(commID, "小区信息");
		if (error != null) {
			return error;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {
			map.put("entity", propFeeCommInfoService.findLastDate(commID));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response initBaseInfo(HttpServletRequest servletRequest,
			String commID) throws Exception {
		Response error = this.validateUUId(commID, "小区信息");
		if (error != null) {
			return error;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {
			propFeeCommInfoService.synchrodata(commID);
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

}
